package cn.zyx.org.service.impl;

import cn.zyx.basic.service.impl.BaseServiceImpl;
import cn.zyx.basic.util.MD5Utils;
import cn.zyx.basic.util.PageList;
import cn.zyx.basic.util.StrUtils;
import cn.zyx.org.domain.Employee;
import cn.zyx.org.mapper.EmployeeMapper;
import cn.zyx.org.query.EmployeeQuery;
import cn.zyx.org.query.FixQuery;
import cn.zyx.org.service.IEmployeeService;
import cn.zyx.user.domain.LoginInfo;
import cn.zyx.user.mapper.LoginInfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通过继承基础接口的实现类，来得到基础接口的实现
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)  // 加上事务
public class EmployeeServiceImpl extends BaseServiceImpl<Employee>
        implements IEmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    LoginInfoMapper loginInfoMapper;

    @Override
    public PageList<Employee> queryFixEmp(FixQuery fixQuery) {
        // 查询总条数
        Integer total = employeeMapper.queryCount(fixQuery);
        // 查询分页数据
        List<Employee> rows = employeeMapper.queryFixEmp(fixQuery);
        return new PageList<>(total,rows);
    }

    /**
     * 店铺入驻，操作员工和店铺的信息的同时，同步操作登录信息表中的数据
     * @param employee
     */
    @Override
    @Transactional
    public void add(Employee employee) {
        // 处理盐值和密码的信息
        initEmployee(employee);
        LoginInfo loginInfo = employee2LoginInfo(employee);
        loginInfoMapper.save(loginInfo);
        employee.setLoginInfo(loginInfo);
        employeeMapper.save(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        // 根据员工的登录信息id去数据库查询
        LoginInfo loginInfo = loginInfoMapper.loadById(employee.getLoginInfo_id());
        if (loginInfo != null){
            // 使用工具类拷贝，同名原则拷贝属性值
            BeanUtils.copyProperties(employee,loginInfo);
            // 修改用户和登录信息表中的数据
            loginInfoMapper.update(loginInfo);
            employeeMapper.update(employee);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 根据id去数据库查询员工的信息
        Employee employee = employeeMapper.loadById(id);
        if (employee != null){
            loginInfoMapper.remove(employee.getLoginInfo_id());
            employeeMapper.remove(id);
        }
    }

    // 保存employee的密码和盐值信息
    private void initEmployee(Employee employee) {
        // 生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 获取加密和加盐的密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        // 重新赋值
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
    }

    private LoginInfo employee2LoginInfo(Employee employee) {
        // 定义需要返回的对象
        LoginInfo loginInfo = new LoginInfo();
        // 封装数据 -- 这里直接使用工具类拷贝，同名原则拷贝属性值 -- 这个工具类也是spring提供的
        BeanUtils.copyProperties(employee, loginInfo);
        // 给loginInfo的type字段赋值 -- 0是员工，1是用户
        loginInfo.setType(0);
        // 返回
        return loginInfo;
    }
}
